package com.jeesite.modules.ic.api;

import com.alibaba.fastjson.JSONObject;
import com.alipay.v3.model.AlipayFundAccountbookNotifyQueryResponseModel;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.config.CardStatus;
import com.jeesite.modules.ic.entity.IcCard;
import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcCardService;
import com.jeesite.modules.ic.service.IcUserService;
import com.jeesite.modules.mo.entity.MoTixian;
import com.jeesite.modules.mo.entity.MoTixianAli;
import com.jeesite.modules.mo.service.MoTixianAliService;
import com.jeesite.modules.mo.service.MoTixianService;
import com.jeesite.modules.mo.service.MoneyService;
import com.jeesite.modules.utils.wx.WxMini;
import com.jeesite.modules.utils.wx.vo.PushCard;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/callback")
@Slf4j
public class CallBackController {

    @Autowired
    private IcCardService icCardService;

    @Autowired
    private IcUserService icUserService;

    @Autowired
    private WxMini wxMini;
    @Autowired
    private MoTixianAliService moTixianAliService;
    @Autowired
    private MoTixianService moTixianService;
    @Autowired
    private MoneyService moneyService;

    /**
     * 泽米卡状态回调
     * @param jsonObject
     * @return
     * {
     *     "previousVal":"1",  //之前值：设备状态变更1:已激活；2:可激活；3:已停用；4:已失效；5:可测试；6:库存；7:已更换；8:已清除;
     *     "currentVal":"3",  //当前值：设备状态变更1:已激活；2:可激活；3:已停用；4:已失效；5:可测试；6:库存；7:已更换；8:已清除;
     *     "reason":0,
     *     "iccid":"8986061509000003300",
     *     "sign":"9430F2B576DABA1C6374CB0FA82B7E29F65527B6FB3E1182474A751B4070AF1C",
     *     "time":"20210428134933",
     *     "serialNo":1380423992295354368,
     *     "type":"DEVICE_STATUS_CHANGE"
     * }
     */
    @RequestMapping("cardStatusCallbackZM")
    public JSONObject cardStatusCallbackZM(@RequestBody JSONObject jsonObject){
        String iccid = jsonObject.getString("iccid");
        IcCard oneCard = icCardService.getOneCard(iccid);
        int status = Integer.valueOf(jsonObject.getString("currentVal"));
        String cardStatus = "";
        String cardStatusStr = "";
        if(status==1){
            cardStatus = CardStatus.ZHENGCHANG.getNum();//正常
            cardStatusStr = CardStatus.ZHENGCHANG.getText();//正常
        }
        if(status==2){
            cardStatus = CardStatus.WEISHIYONG.getNum();//静默期
            cardStatusStr = CardStatus.WEISHIYONG.getText();//正常
        }
        if(status==3){
            cardStatus = CardStatus.TINGYONG.getNum();//已停用
            cardStatusStr = CardStatus.TINGYONG.getText();//正常
        }
        if(status==4){
            cardStatus = CardStatus.SHIXIAO.getNum();//已失效
            cardStatusStr = CardStatus.SHIXIAO.getText();//正常
        }
        if(status==5){
            cardStatus = CardStatus.WEISHIYONG.getNum();//测试期
            cardStatusStr = CardStatus.WEISHIYONG.getText();//正常
        }
        if(status==6){
            cardStatus = CardStatus.WEISHIYONG.getNum();//静默期
            cardStatusStr = CardStatus.WEISHIYONG.getText();//正常
        }
        if(status==7){
            cardStatus = CardStatus.SHIXIAO.getNum();//已失效
            cardStatusStr = CardStatus.SHIXIAO.getText();//正常
        }
        if(status==8){
            cardStatus = CardStatus.TINGYONG.getNum();//停用
            cardStatusStr = CardStatus.TINGYONG.getText();//正常
        }
        oneCard.setCardStatus(cardStatus);
        icCardService.save(oneCard);
        //推送
        String userId = oneCard.getUserId();
        if (StringUtils.isNotEmpty(userId)){
            IcUser icUser = icUserService.get(userId);
            if (icUser != null){
                String wxOpenid = icUser.getWxOpenid();
                if(StringUtils.isNotEmpty(wxOpenid)){
                    PushCard pushCard = new PushCard();
                    pushCard.setCardId(oneCard.getId());
                    pushCard.setOpenId(wxOpenid);
                    pushCard.setIccid(oneCard.getIccid());
                    pushCard.setTime(DateUtils.getDateTime());
                    pushCard.setTitle(oneCard.getName());
                    pushCard.setImei(oneCard.getImei());
                    pushCard.setContent("当前流量卡"+cardStatusStr);
                    wxMini.pushmsg(pushCard);
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("msg", "成功");
        result.put("success", true);
        return result;
    }

    /**
     * 支付宝通知回调
     * @param
     * @return
     */
    @RequestMapping("aliPayCallback")
    public String aliPayCallback(HttpServletRequest request,HttpServletResponse response){
        String bizContent = request.getParameter("biz_content");
        JSONObject bizContentJSON = JSONObject.parseObject(bizContent);
        log.error("支付宝参数："+bizContentJSON.toJSONString());
        if(bizContentJSON!=null && StringUtils.isNotEmpty(bizContentJSON.getString("status"))){
            if("SUCCESS".equals(bizContentJSON.getString("status"))){
                String tixianId = bizContentJSON.getString("out_biz_no");
                String orderId = bizContentJSON.getString("order_id");
                MoTixianAli moTixianAli = new MoTixianAli();
                moTixianAli.setTixianId(tixianId);
                moTixianAli.setAliOrderId(orderId);
                List<MoTixianAli> list = moTixianAliService.findList(moTixianAli);
                for(MoTixianAli moTixianAli1 : list){
                    moTixianAli1.setPayDate(DateUtils.parseDate(bizContentJSON.getString("pay_date")));
                    moTixianAli1.setAliStatus("SUCCESS");
                    moTixianAliService.save(moTixianAli1);
                }
                MoTixian moTixian = moTixianService.get(tixianId);
                moTixian.setTixianStatus("2");
                moTixianService.save(moTixian);
                moneyService.synUserSumShouyi(moTixian.getUserId());
            }
        }


        return "success";
    }


}
